package top.yvyan.guettable.data;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.tencent.mmkv.MMKV;

import java.util.Date;
import java.util.List;

import top.yvyan.guettable.Gson.Semester;
import top.yvyan.guettable.util.TimeUtil;

public class GeneralData {
    private static GeneralData generalData;
    private static final String SHP_NAME = "GeneralData";
    private static final String NAME = "name";
    private static final String NUMBER = "number";
    private static final String MAX_WEEK = "maxWeek";
    private static final String WEEK = "week";
    private static final String TIME = "time";
    private static final String GRADE = "grade";
    private static final String TERM = "term";

    private static final String SEMESTER_ID = "SemesterId";

    private static final String ADD_TERM = "addTerm";
    private static final String LAST_UPDATE_TIME = "lastUpdateTime";
    private static final String APPLY_PRIVACY = "applyPrivacy";
    private static final String WIDGET_THEME = "widget_theme";
    private static final String WIDGET_ALPHA = "widget_alpha";

    private static final String AUTO_TERM_OPEN = "auto_term_open";
    private static final String AUTO_TERM_START_TIME = "auto_term_startTime";
    private static final String AUTO_TERM_END_TIME = "auto_term_endTime";

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    private String name;
    private String number;
    private int maxWeek;
    private String grade;

    private Integer semesterId;

    private long lastUpdateTime;
    //隐私协议
    private boolean applyPrivacy;

    private String widget_theme;
    private int widget_alpha;

    private static final MMKV mmkv = MMKV.defaultMMKV();

    @SuppressLint("CommitPrefEdits")
    private GeneralData(Context context) {
        sharedPreferences = context.getSharedPreferences(SHP_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        load();
    }

    private void load() {
        name = sharedPreferences.getString(NAME, "");
        number = sharedPreferences.getString(NUMBER, "");
        maxWeek = sharedPreferences.getInt(MAX_WEEK, 20);
        grade = sharedPreferences.getString(GRADE, null);
        semesterId = sharedPreferences.getInt(SEMESTER_ID, 65);
        lastUpdateTime = sharedPreferences.getLong(LAST_UPDATE_TIME, -1);
        applyPrivacy = sharedPreferences.getBoolean(APPLY_PRIVACY, false);
        widget_theme = sharedPreferences.getString(WIDGET_THEME, "black");
        widget_alpha = sharedPreferences.getInt(WIDGET_ALPHA, 255);
    }

    public static GeneralData newInstance(Context context) {
        if (generalData == null) {
            generalData = new GeneralData(context);
        }
        return generalData;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        editor.putString(NAME, name);
        editor.apply();
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
        editor.putString(NUMBER, number);
        editor.apply();
    }

    public int getMaxWeek() {
        return maxWeek;
    }

    public void setMaxWeek(int maxWeek) {
        this.maxWeek = maxWeek;
        editor.putInt(MAX_WEEK, maxWeek);
        editor.apply();
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
        editor.putString(GRADE, grade);
        editor.apply();
    }

    public Semester getSemester() {
        Semester semester = MoreData.getSemester();
        if (semester == null) {
            semester = querySemester(semesterId);
            if (semester!=null) setSemester(semester);
        }
        return semester;
    }

    public void setSemester(Semester semester) {
        MoreData.setSemester(semester);
    }

    public Semester querySemester(int semesterId) {
        List<Semester> semesters = MoreData.getSemesterList();
        for (Semester semester : semesters) {
            if (semester.id == semesterId) {
                return semester;
            }
        }
        return null;
    }

    public String getTerm() {
        Semester semester = getSemester();
        if (semester!=null) return semester.toString();
        return "Unknown";
    }

    public long getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(long lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(LAST_UPDATE_TIME, lastUpdateTime);
        editor.apply();
    }

    public boolean isApplyPrivacy() {
        return applyPrivacy;
    }

    public void setApplyPrivacy(boolean applyPrivacy) {
        this.applyPrivacy = applyPrivacy;
        editor.putBoolean(APPLY_PRIVACY, applyPrivacy);
        editor.apply();
    }

    public String getWidget_theme() {
        return widget_theme;
    }

    public void setWidget_theme(String widget_theme) {
        this.widget_theme = widget_theme;
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(WIDGET_THEME, widget_theme);
        editor.apply();
    }

    public int getWidget_alpha() {
        return widget_alpha;
    }

    public void setWidget_alpha(int widget_alpha) {
        this.widget_alpha = widget_alpha;
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(WIDGET_ALPHA, widget_alpha);
        editor.apply();
    }

    public static void setAutoTerm(boolean open) {
        mmkv.encode(AUTO_TERM_OPEN, open);
    }

    public static boolean isAutoTerm() {
        return mmkv.decodeBool(AUTO_TERM_OPEN, false);
    }

    public Date getStartTime() {
        Semester semester = getSemester();
        if (semester != null) {
            return new Date(semester.getStartTime());
        }
        long time = mmkv.getLong(AUTO_TERM_START_TIME, 0);
        return new Date(time);
    }

    public Date getEndTime() {
        Semester semester = getSemester();
        if (semester != null) {
            return new Date(semester.getEndTime());
        }
        long time = mmkv.getLong(AUTO_TERM_END_TIME, 0);
        if (time == 0) {
            return new Date(getStartTime().getTime() + ((long) getMaxWeek() * 86400L * 7L * 1000L));
        }
        return new Date(time);
    }


    /**
     * 自动获取星期数
     *
     * @return 星期数
     */
    public int getWeek() {
        return getWeek(false);
    }

    public int getWeek(boolean allowNegative) {
        Date startTime = getStartTime();
        Date now = new Date();
        if (!allowNegative && startTime.after(now)) {
            return 1;
        } else {
            return TimeUtil.calcWeekOffset(startTime, now) + 1;
        }
    }
}
